#!/usr/bin/perl

package Swignition::Export::Taxonomy;
use Swignition::Export;
use JSON 2.0;

sub is
{
	my $subject = shift;
	return unless (ref $subject);
	return
		unless (defined $subject->property('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));
	foreach my $Type ( @{$subject
		->property('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
		->valObjects} )
	{
		my $type = $Type->toString;
		return 1
			if (($type eq 'http://purl.org/NET/biol/ns#Taxonomy')
			||  ($type eq 'http://purl.org/NET/biol/ns#BotTaxonomy')
			||  ($type eq 'http://purl.org/NET/biol/ns#ZooTaxonomy'));
	}
	return;
}

sub outputs
{
	return [
		{
			code  => 'Text',
			name  => 'Plain text representation',
			type  => 'text/plain',
			multi => 0
		}
	];
}

sub outText
{
	my $triples = shift;
	my $subject = shift;
	my $S = findSubject($triples, $subject);
	
	my $name = firstOfLiteral($S,
		['http://purl.org/NET/biol/ns#name'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);

	if (!length $name)
	{
		my $n1 = firstOfLiteral($S,
			['http://purl.org/NET/biol/ns#genus'],
			['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
		my $n2 = firstOfLiteral($S,
			['http://purl.org/NET/biol/ns#species'],
			['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
		my $n3 = firstOfLiteral($S,
			['http://purl.org/NET/biol/botany#subspecies',
			'http://purl.org/NET/biol/zoology#subspecies'],
			['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
			
		if (length $n1 && length $n2 && length $n3)
			{ $name = "$n1 $n2 $n3"; }
		elsif (length $n1 && length $n2)
			{ $name = "$n1 $n2"; }
	}
	
	if (length $name)
	{
		my $auth = firstOfLiteral($S,
			['http://purl.org/NET/biol/ns#authority',
			'http://purl.org/NET/biol/botany#authority',
			'http://purl.org/NET/biol/zoology#authority'],
			['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
				
		return "$name ($auth)"
			if (length $auth);
			
		return $name;
	}
	
	return firstOfLiteral($S,
		['http://purl.org/NET/biol/ns#commonName',
		'http://www.w3.org/2000/01/rdf-schema#label'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
}


1;
